﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using loginSQL.Pages;
using System.Threading;
using loginSQL.SQLHelper;
using loginSQL.Util.Network;

namespace loginSQL.Window
{
    public partial class SQLSetting : Form
    {
        private bool isKonekDatabaseSuccess = false;
        private bool isKonekServerSuccess = false;

        delegate void konekDatabaseDelegate();
        delegate void konekServerDelegate();

        private MySQLSettings settings;

        public SQLSetting()
        {
            InitializeComponent();
            this.txt_servername.Text = Constants.serverName;
            this.txt_usernameSQL.Text = Constants.usernameSQL;
            this.txt_passwordSQL.Text = Constants.passwordSQL;
            this.txt_port.Text = Constants.port;
            this.txt_dbname.Text = Constants.databaseName;
            this.txt_server_pusat.Text = Constants.server_pusat;

            //initCekKoneksi();
            
        }

        

        private void button1_Click(object sender, EventArgs e)
        {
            if (txt_servername.Text != "" || txt_usernameSQL.Text != "" || txt_passwordSQL.Text != "" || txt_port.Text !="" || txt_server_pusat.Text != "")
            {
                Constants.serverName = txt_servername.Text;
                Constants.usernameSQL = txt_usernameSQL.Text;
                Constants.passwordSQL = txt_passwordSQL.Text;
                Constants.port = txt_port.Text;
                Constants.databaseName = txt_dbname.Text;
                Constants.server_pusat = txt_server_pusat.Text;

                try
                {
                    int port = Convert.ToInt32(txt_port.Text);
                    if (port < 1 || port > 65556)
                    {
                        MessageBox.Show("Port Number is Invalid");
                        Constants.port = "3306";
                    }
                    
                }
                catch
                {
                    MessageBox.Show("Port Number is Invalid");
                    Constants.port = "3306";
                }
                SQLHelper.SaveLoadConfig.Save(new SQLHelper.MySQLSettings(Constants.serverName,Constants.port,Constants.usernameSQL,Constants.passwordSQL,Constants.databaseName),Constants.server_pusat);
                this.Close();
            }
            else
            {
                MessageBox.Show("Input Invalid");
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void btn_cek_koneksi1_Click(object sender, EventArgs e)
        {

            showLoadingKonekDatabase();
            settings = new MySQLSettings(txt_servername.Text, txt_port.Text, txt_usernameSQL.Text, txt_passwordSQL.Text,txt_dbname.Text);
            db_konek.RunWorkerAsync();
        }

        

        private void cekKoneksi()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new konekDatabaseDelegate(cekKoneksi));
                return;
            }
            showLoadingKonekDatabase();
            if (isKonekDatabaseSuccess)
            {
                isKonekDatabaseSuccess = false;
                MessageBox.Show("Koneksi Berhasil");
            }
            else
            {
                MessageBox.Show("Koneksi Gagal");
            }
        }

        private void db_konek_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(1000);
            if (DBHelper.isDatabaseAvailable(settings))
            {
                isKonekDatabaseSuccess = true;

            }
            cekKoneksi();

        }

        private void server_konek_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(1000);
            if (PingUtility.tryPing(txt_server_pusat.Text))
            {
                isKonekServerSuccess = true;

            }
            cekKoneksiServer();
        }

        private void cekKoneksiServer()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new konekServerDelegate(cekKoneksiServer));
                return;
            }
            showLoadingKonekServer();
            if (isKonekServerSuccess)
            {
                isKonekServerSuccess = false;
                MessageBox.Show("Koneksi Berhasil");
            }
            else
            {
                MessageBox.Show("Koneksi Gagal");
            }
        }

        private void showLoadingKonekDatabase()
        {
            img_loading1.Visible = !img_loading1.Visible;
        }

        private void showLoadingKonekServer()
        {
            img_loading2.Visible = !img_loading2.Visible;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_cek_koneksi2_Click(object sender, EventArgs e)
        {
            showLoadingKonekServer();
            server_konek.RunWorkerAsync();
        }
    }
}

